//
// Created by shaun on 8/5/2025.
//

#ifndef CONTROL_PANEL_H
#define CONTROL_PANEL_H

#include <QWidget>
#include <QSpinBox>
#include <QPushButton>
#include <QGridLayout>
#include <QJsonObject>
#include <QJsonDocument>
#include <QFile>
#include <QStandardPaths>
#include <QDir>
#include <QComboBox>
#include "generic_controller.h"

namespace eintik::presentation::panel
{

class ControlPanel final : public QWidget
{
    Q_OBJECT

public:
    explicit ControlPanel(QWidget* parent = nullptr);
    ~ControlPanel() override;

    using KeyPairMapType = std::unordered_map<eintik::KeyType, eintik::ValueType>;

    // component
    QSpinBox* KStartElement() const { return kStartElement; }
    QDoubleSpinBox* KGapElement() const {return kGapElement;}
    QSpinBox* KEndElement() const { return kEndElement; }
    QDoubleSpinBox* KGainDb() const { return kGainDb; }
    QSpinBox*KBeamLocation() const { return kBeamLocation; }
    QSpinBox* KPaVoltage() const { return kPaVoltage; }
    QSpinBox* KAperture() const { return kAperture; }
    QSpinBox* KPulseWidthNs() const { return kPulseWidthNs; }
    QSpinBox* KStep() const { return kStep; }
    QSpinBox* KFrameRate() const { return kFrameRate; }
    QDoubleSpinBox* KScanStartUs() const { return kScanStartUs; }
    QDoubleSpinBox* KScanRangeUs() const {return kScanRangeUs; }
    QSpinBox* KWaveSpeed() const { return kWaveSpeed; }
    QSpinBox* KSampleQty() const { return kSampleQty; }

    QDoubleSpinBox* KGateAStart() const { return kGateAStart; }
    QDoubleSpinBox* KGateAEnd() const { return kGateAEnd; }
    QDoubleSpinBox* KGateBStart() const { return kGateBStart; }
    QDoubleSpinBox* KGateBEnd() const { return kGateBEnd; }
    QDoubleSpinBox* KGateIHight() const { return kGateIHight; }


    QDoubleSpinBox* KCScanInterval() const { return kCScanInterval; }
    QSpinBox* KStartParamSpinBox() const { return kStartParamSpinBox; }

    // buttons
    QPushButton* startButton() const { return start_btn_; }
    QPushButton* stopButton() const { return stop_btn_; }
    QPushButton* setButton() const { return set_btn_; }
    QPushButton* configButton() const { return config_btn_; }
    QPushButton* connectButton() const { return connect_btn_; }
    QPushButton* closeButton() const { return close_btn_; }

    // global layout
    QGridLayout* focalLawLayout() const { return focal_law_layout_; }
    QGridLayout* controlLayout() const { return control_layout_; }
    QGridLayout* networkLayout() const { return network_layout_; }
    QGridLayout* debugLayout() const { return debug_layout_; }

    KeyPairMapType CollectValues() const;

public slots:
    void OnWidgetValuesChanged() const;

    void EmitParameterChanged();

    void SetWidgetBinding(const KeyType& key, QSpinBox* box);

    void SetWidgetBindingDouble(const KeyType& key, QDoubleSpinBox* box);

    void OnCollectValues(std::unordered_map<KeyType, ValueType>& params) const;

    // 新增：保存参数到JSON
    void SaveParamsToJson();

    // 新增：从JSON加载参数到UI
    void LoadParamsFromJson();

signals:
    void ParameterChanged(const std::unordered_map<KeyType, ValueType>& params);

private:
    QSpinBox* kStartElement;
    QDoubleSpinBox* kGapElement;
    QSpinBox* kEndElement;
    QDoubleSpinBox * kGainDb;
    QSpinBox* kBeamLocation;
    QSpinBox* kPaVoltage;
    QSpinBox* kAperture;
    QSpinBox* kPulseWidthNs;
    QSpinBox* kStep;
    QSpinBox* kFrameRate;
    QDoubleSpinBox* kScanStartUs;
    QDoubleSpinBox* kScanRangeUs;
    QSpinBox* kWaveSpeed;
    QSpinBox* kSampleQty;

    QDoubleSpinBox* kGateAStart;
    QDoubleSpinBox* kGateAEnd;
    QDoubleSpinBox* kGateBStart;
    QDoubleSpinBox* kGateBEnd;
    QDoubleSpinBox* kGateIHight;

    QDoubleSpinBox* kCScanInterval;

    QComboBox* kStartTypeCombox;
    QSpinBox* kStartParamSpinBox;

    QGridLayout* focal_law_layout_;

    // Control component
    QPushButton* start_btn_;
    QPushButton* stop_btn_;
    QPushButton* set_btn_;
    QPushButton* config_btn_;

    // Network component
    QPushButton* connect_btn_;
    QPushButton* close_btn_;

    // Debug component
    QPushButton* debug_btn_;

    QGridLayout* control_layout_;
    QGridLayout* network_layout_;
    QGridLayout* debug_layout_;

    QTimer* debounce_timer_{};
    std::unordered_map<KeyType, QSpinBox*> spin_bindings_;
    std::unordered_map<KeyType, QDoubleSpinBox*> double_bindings_;

    // 新增：获取JSON文件路径（AppData目录下）
    QString GetJsonFilePath() const;
    // 新增：检查JSON文件是否存在
    bool JsonFileExists() const;
    // 新增：创建空的JSON文件
    void CreateEmptyJsonFile() const;
};

}

#endif //CONTROL_PANEL_H
